*Nov 17 version
*Get rid of missing wages (self-employed) 
*Update all the way to 2016 
*5 segments instead of 10
*Quadratic provincial trends
*Prov*bin and year*bin as in triple diff
*full prov*dummies in lower segment
*Add dummy for integer dollar value
*allow for different effects just after increase



/*******************************
This program is the same as James' code 
except here I use 3 month dummy variable.

That is, dummy for upcoming minimum wage change.


*******************************/










* This version of the code estimates full models for men and women with and without
* the "B" restrictions.log using $logdir\Townsend_hzd_sep_5_2014,text replace

clear
discard
cap log close

set more off
set matsize 800
*set mem 1200m

/* "pub" is a flag for testing the code using the public use files and should be always set to zero when using the data in the RDC. */
local pub=0



* workdata is the directory that contains the working data sets
global workdatadir "P:\Townsend_5609_wages\November2019\WorkingData"
global logdir "P:\Townsend_5609_wages\November_2023\logfiles"
global resultdir "P:\Townsend_5609_wages\November_2023\results"

*global workdatadir "P:\Townsend_3234\data"
*global logdir "P:\Townsend_3234\STATA_code\log_files"
*global resultdir "P:\Townsend_3234\results"

log using $logdir\hzd_estimator_November_6_2023_joiners_main.log, replace



/****************************************************
* This is the old data version of min wage changes


/******TL: CREATE DATA SET KEEPING TRACK OF MW CHANGES******/
u $workdatadir\min_wag_1996_2017
sort prov year month

by prov: gen mw=mwage[_n-3]
gen chg=((mwage-mw)/mw)
gen mwchg=chg>0 /*could play with this*/




by prov: gen l5mw=mwage[_n-12]
gen chg=((mwage-l5mw)/l5mw)
gen mwchg=chg>0 /*could play with this*/



keep if year>=1997 & year<=2016
/*****JT: Moved to monthly************/
keep prov year month mwchg
sort prov year month
save $workdatadir\mwchg, replace
clear
/*END OF MW CHG LOOP*/

****************************************************/

/*****************************************************
Create dummy for upcoming min wage change for leavers
****************************************************/

u $workdatadir\min_wag_1996_2017 ,clear

* Get dummy for min wage change coming in next 3 months

bys prov (year month): g time=_n
tsset prov time,delta(1)

g change=mwage-L.mwage
replace change=1 if change!=0 & change!=.
g temp=change

g mwchg=1 if temp==1 
replace mwchg=1 if L.temp==1 
replace mwchg=0 if mwchg!=1
replace mwchg=. if time==1
drop temp

keep if year>=1997 & year<=2016
keep prov year month mwchg
save $workdatadir\mwchg_current.dta ,replace



/*****************************************************
Get new minimum wage in third month
****************************************************/

use $workdatadir\min_wag_1996_2017,clear

keep prov year month cpi mwage
bys prov (year month): g time=_n
tsset prov time,delta(1)

g mwage3=mwage
g cpi3=cpi

keep if year>=1997 & year<=2016
keep prov year month mwage3 cpi3

save $workdatadir\min_wag_1996_2017_month3.dta,replace


use $workdatadir\min_wag_1996_2017,clear

keep prov year month cpi mwage
bys prov (year month): g time=_n
tsset prov time,delta(1)

g mwage3=L2.mwage
g cpi3=L2.cpi

keep if year>=1997 & year<=2016
keep prov year month mwage3 cpi3
ren cpi3 cpi
ren mwage3 mwage

save $workdatadir\min_wag_1996_2017_month1.dta,replace






/*Estimation loops begin here. */

local sex=1


while `sex'<=2 {

	* `k' is the sample counter for broad criteria (see below)

	local k=1
	/*k indicates the data set to be created
	Currently:  1 = full data set
				2 = only those paid by the hour.
				3 = excluding those earning tips. */






	* `j' is the subsample counter (see below)



foreach j of numlist 1 2 4 {

		* Loads base data set - additional restrictions to follow.
		if `pub'~=0 use $workdatadir\est_dat,clear
		if `pub'~=1 use $workdatadir\panel_6M_sample,clear

		
		/* Drop observations with partial imputation, etc. */

		drop if flag_restrictionsB==1
		*Testing restrictions

		display `data after flag_restrictionsB applied'
		sum

		/* Gender selection */
		if `sex'==1 {
			keep if female==0
		}

		if `sex'==2 {
			keep if female==1
		}

		/*Additional possible deletion criteria */

		if `k'==2 {
			/*Normally paid by hour*/
			keep if hourlywork1==1
		}

		if `k'==3 {
			/*No tips */
			keep if tips1~=1
		}

		/*Subsample criteria based on `j' counter */

		/* Note: j=1 has no restrictions - full data set */
		
		if `j'==2 {
			/*New hires (less than 3 months of job tenure in first month of mini-panel*/
			keep if tenure1<3
		}

		
		if `j'==3 {
			/*New hires (up to 12 months of job tenure in first month of mini-panel*/
			keep if tenure1<=12
		}
		

		if `j'==4 {
			/* Stayers*/
			keep if (lfsstat1 <= 2 & tenure1 >= 3)
		}
		
		if `j'==5 {
			/* Stayers with up to one year of tenure at start of previous quarter */
			keep if (lfsstat1 <= 2 & tenure1 >= 3 & tenure1<=14)
		}
		
		if `j'==6 {
			/* Stayers with >1 to 5 years of tenure at start of previous quarter*/
			keep if (lfsstat1 <= 2 & tenure1 >= 15 & tenure1<=62)
		}
		
		if `j'==7 {
			/* Stayers with >5 years of tenure at start of previous quarter*/
			keep if (lfsstat1 <= 2 & tenure1 >= 63)
		}
	

		*if `j'==6 {
		*	/*Old hires (more than 12 months of job tenure in first month of mini-panel*/
		*	keep if tenure1>12
		*}

		display `sex' `j' `k'

		display `data after additional restrictions applied'
		sum

		*sum female lfsstat1 lfsstat2 hourlywork1 tips1




		*sum age female educlev prov wage1 tenure1 [aw=finalwt1]

		*Sample restrictions beyond basicsdrop if flag_restrictionsB==1

		rename smth1 month
		rename syear1 year

		sort prov year month


		/*merge prov year month using P:\Townsend_3234\data\min_wag_2013*/
		/*****TL: NEW MIN WAGE DATA SET FOR 2016*****/
		merge m:1 prov year month using $workdatadir\min_wag_1996_2017_month1
		drop if _merge==2
		drop _merge

		merge m:1 prov year month using $workdatadir\min_wag_1996_2017_month3
		drop if _merge==2
		drop _merge

		gen rdvwag=wage1/cpi*100
		/*****TL: DEFLATE THE MINIMUM WAGE, WAS ALREADY DONE FOR 1997-2011*/	
		gen rminw=mwage/cpi*100  
		gen rminw3=mwage3/cpi3*100  

		gen agecat=.
		replace agecat=1 if age>=15&age<=19
		replace agecat=2 if age>=20&age<=24
		replace agecat=3 if age>=25&age<=34
		replace agecat=4 if age>=35&age<=54
		replace agecat=5 if age>=55&age<=60

		drop if agecat==.



		label define AGECAT 1 "15-19" 2 "20-24" 3 "25-34" 4 "35-54" 5 "55-60"
		label value agecat AGECAT

		gen edgrp=.
		replace edgrp=1 if educlev>=0&educlev<=2
		replace edgrp=2 if educlev==3|educlev==4
		replace edgrp=3 if educlev>=5&educlev<=7
		replace edgrp=4 if educlev==8|educlev==9

		drop if edgrp==.

		table edgrp educlev

		label define EDGRP 1 "Less than H.S." 2 "H.S. graduate and/or some P.S." 3 "P.S. Diploma or Cert." 4 "B.A. or more"
		label value edgrp EDGRP






		************************************************************
		*Code for lagged minimum wage

		*preserve

		*collapse mwage rminw,by(prov year)
		*gen mwage1=mwage[_n-1]
		*gen rminw1=rminw[_n-1]
		 

		*save d:\min_wag\data\min_one_year,replace
		*restore
		*rename _merge old_merge
			
		*merge m:1 prov year using d:\min_wag\data\min_one_year
		************************************************************

		gen unemp=1-(lfsstat1<=2)
		*keep if unemp==0
		sum unemp
		*gen ftstudent=schooln==2|schooln==4|schooln==6|schooln==8

		*drop if ftstudent==1

		*keep if age_12<=3

		*keep if educ90<=2
		*keep if cowmain<=2

		*keep if educlev<=3


		set more off

		gen wagcat=0
		replace wagcat=1 if rdvwag<=3
		replace wagcat=2 if rdvwag>3 & rdvwag<=3.5
		replace wagcat=3 if rdvwag>3.5 & rdvwag<=4



		/****TL: EXPAND LOOP TO FIND IF INTEGER VALUE IN THE WAGE CATEGORY*/

		local i=1
		while `i'<=160{
			replace wagcat=3+`i' if rdvwag>4+(`i'-1)*.1 & rdvwag<=4+`i'*.1
			local i=`i'+1
		}

		replace wagcat=164 if rdvwag>20
		*replace wagcat=145 if unemp==1
		*gen delta=(wagcat==145)
		*min wage & lages assigned here

		/***************JT: Fill in data set for identifying integer. */
		preserve
		sort prov year month wagcat
		collapse cpi,by(prov year month wagcat)

		fillin prov year month wagcat
		sum _fillin

		egen cpimax=max(cpi),by(prov year month)
		replace cpi=cpimax if cpi==.
		gen cut_l=0
		gen cut_h=0
		replace cut_l=0 if wagcat==1
		replace cut_l=3*cpi/100 if wagcat==2
		replace cut_l=3.5*cpi/100 if wagcat==3
		replace cut_h=3*cpi/100 if wagcat==1
		replace cut_h=3.5*cpi/100 if wagcat==2
		replace cut_h=4*cpi/100 if wagcat==3
		local i=1
		while `i'<=160{
			replace cut_l=(4+(`i'-1)*.1)*cpi/100 if wagcat==`i'+3	
			replace cut_h=(4+`i'*.1)*cpi/100 if wagcat==`i'+3
			local i=`i'+1
		}
		drop if cpi==.

		sum

		/****TL: INTEGER VALUE IN THE WAGE CATEGORY*/
		gen dollar=(floor(cut_h)>cut_l)
		summ dollar cut_l cut_h

		/*Could include province in list if cpi was done at the monthly level...*/
		keep if prov==35
		keep year month wagcat dollar
		sum
		sort year month wagcat
		table year month
		save $workdatadir\dollar_bins,replace

		restore





	*	table wagcat,c(min rdvwag max rdvwag)
		 


		gen mincat=0
		replace mincat=1 if rminw<=3
		replace mincat=2 if rminw>3 & rminw<=3.5
		replace mincat=3 if rminw>3.5 & rminw<=4

		local i=1
		while `i'<=76{
			replace mincat=3+`i' if rminw>4+(`i'-1)*.1 & rminw<=4+`i'*.1
			local i=`i'+1
		}

		*Check for rank of mincat
		sum female mincat
		gen mincat1=0


		


		gen mincatn=0
		replace mincatn=1 if rminw3<=3
		replace mincatn=2 if rminw3>3 & rminw3<=3.5
		replace mincatn=3 if rminw3>3.5 & rminw3<=4

		local i=1
		while `i'<=76{
			replace mincatn=3+`i' if rminw3>4+(`i'-1)*.1 & rminw3<=4+`i'*.1
			local i=`i'+1
		}



		*table prov year,c(mean plus mean minus)

		 
		gen one=1


		 
		rename finalwt1 fweight 
		 
		rename agecat agegrp 

		compress 
		 
		sort prov year month female agegrp edgrp wagcat mincat mincatn
		collapse mincat1 (sum) fweight (sum) one,by(prov year month female agegrp edgrp wagcat mincat mincatn) 

		fillin prov year month female agegrp edgrp wagcat 

		*Checks for obs where entire spell is created by fillin command.
		egen filled=sum(_fillin),by(prov year month female agegrp edgrp)
		sum filled
		drop if filled==164
		sum filled

		egen minmax=max(mincat),by(prov year month)
		replace mincat=minmax if mincat==.
		drop minmax

		egen minmax=max(mincatn),by(prov year month)
		replace mincatn=minmax if mincatn==.

		sort year month wagcat
		merge m:1 year month wagcat using $workdatadir\dollar_bins
		drop _merge


		*egen minmax1=max(mincat1),by(prov year month)
		*replace mincat1=minmax1 if mincat1==.

		*Fill in bins with no casualties*
		replace fweight=0 if fweight==.


		gen sub=prov==12|prov==35|(prov==59&((year==2001&month>=11)|(year>=2002&year<=2011)|(year==2012&month<5)))



		sum dollar
		summ dollar [w=fweight]

		/*****TL: MERGE TO MW CHG TO CREATE NEW DUMMIES*****/

		/*****IS: NEED TO CHANGE FILE HERE AS REQUIRED*****/
		sort prov year month
		merge m:1 prov year month using $workdatadir\mwchg_current
		drop _merge
		
		//  CONFIRM THIS
		// Don't want duplicate observations for cases where there is no min wage change
		replace mincatn=0 if mwchg==0	

		

		
		/********JT: removed sub as a criterion....not used below*/

		sort prov year month female agegrp edgrp wagcat mincat mincatn dollar mwchg

		collapse (sum) fweight (sum) one,by(prov year month female agegrp edgrp wagcat mincat mincatn dollar mwchg) 

		compress


		/*Insert estimation code here */

		set more off


		*Fillin command creates observations for early 1996 as well, which have no obs. from the LFS.
		*also observations in 2012 (December) that are empty.
		drop if mincat==. & year==1996
		drop if mincat==. & year==2016
		drop if year==1996 /*currently missing 1995 minimum wages.*/
		*Drop if group has failed in earlier bins.


		*Selection criteria


		*gen month=11
		*gen sub=prov==12|prov==35|(prov==59&((year==2001&month>=11)|(year>=2002&year<=2011)|(year==2012&month<5)))
		*drop month

		gen diff=wagcat-mincat
		gen diff2=wagcat-mincatn


		gen min6b1=diff<-5
		gen min35b1=diff>=-5&diff<=-3
		gen min12b1=diff==-2|diff==-1
		gen min1=diff==0
		gen min12a1=diff>=1&diff<=2
		gen min35a1=diff>=3&diff<=5
		gen min610a1=diff>=6&diff<=10
		gen min1115a1=diff>=11&diff<=15
		gen min1620a1=diff>=16&diff<=20
		gen min2125a1=diff>=21&diff<=25
		gen min2630a1=diff>=26&diff<=30	
		gen min3135a1=diff>=31&diff<=35
		gen min3640a1=diff>=36&diff<=40	
		gen min4145a1=diff>=41&diff<=45
		gen min4650a1=diff>=46&diff<=50	
		
		
		/****IS: Bins around old and new min wage****/
		gen minold2new=1 if diff>0 & diff2<0
		replace minold2new=0 if minold2new!=1
		gen minnew=diff2==0

		gen min12a2=diff2>=1&diff2<=2
		gen min35a2=diff2>=3&diff2<=5
		gen min610a2=diff2>=6&diff2<=10
		gen min1115a2=diff2>=11&diff2<=15
		gen min1620a2=diff2>=16&diff2<=20
		gen min2125a2=diff2>=21&diff2<=25
		gen min2630a2=diff2>=26&diff2<=30
		gen min3135a2=diff2>=31&diff2<=35
		gen min3640a2=diff2>=36&diff2<=40
		gen min4145a2=diff2>=41&diff2<=45
		gen min4650a2=diff2>=46&diff2<=50

		*gen smin6b=min6b*sub
		*gen smin35b=min35b*sub
		*gen smin12b=min12b*sub
		*gen smin=min*sub
		*gen smin13a=min13a*sub
		*gen smin410a=min410a*sub
		*gen smin1120a=min1120a*sub
		*gen smin2130a=min2130a*sub
		*gen smin3140a=min3140a*sub
		*gen smin4150a=min4150a*sub



		gen cut=wagcat

		/****TL: FIVE SEGMENTS INSTEAD OF TEN****/
			gen part0=.
			  replace part0=1 if cut<=63  /*up to $10*/
			  replace part0=2 if cut>=64&cut<=88 /*$10 to $12.5*/
			  replace part0=3 if cut>=89&cut<=113 /*$12.5 to $15.0*/
			  replace part0=4 if cut>=114&cut<=138 /*$15 to $17.5*/
			  replace part0=5 if cut>=139&cut<=163 /*$17.5 to $20*/
			*replace part0=1 if cut<=33  /*up to $7*/
			*replace part0=2 if cut>=34&cut<=43 /*$7 to $8*/
			*replace part0=3 if cut>=44&cut<=53 /*$8 to $9*/
			*replace part0=4 if cut>=54&cut<=63 /*$9 to $10*/
			*replace part0=5 if cut>=64&cut<=73 /*$10 to $11*/
			*replace part0=6 if cut>=74&cut<=83 /*$11 to $12*/
			*replace part0=7 if cut>=84&cut<=98 /*$12 to $13.5*/
			*replace part0=8 if cut>=99&cut<=113 /*$13.5 to $15*/
			*replace part0=9 if cut>=114&cut<=138 /*$15 to $17.5*/
			*replace part0=10 if cut>=139&cut<=163 /*$17.5 to $20*/
			*replace part0=11 if cut==164 /*$20 and above*/

		gen part=.
			replace part=1 if cut<=36
			replace part=2 if cut>=37&cut<=42
			replace part=3 if cut>=43&cut<=48
			replace part=4 if cut>=49&cut<=55
			replace part=5 if cut>=56&cut<=64
			replace part=6 if cut>=65&cut<=74
			replace part=7 if cut>=75&cut<=88
			replace part=8 if cut>=89&cut<=103
			replace part=9 if cut>=104&cut<=124
			replace part=10 if cut>=125&cut<=163
			replace part=11 if cut==164

			
			
			
		

		*removed sub...added mwchg
		sort prov year month agegrp edgrp mincat mincatn mwchg
		sort prov year month agegrp edgrp mincat mincatn mwchg wagcat
		bys prov year month agegrp edgrp mincat mincatn mwchg (wagcat): g rightsum=sum(fweight)
		bys prov year month agegrp edgrp mincat mincatn mwchg (wagcat): egen totalsum=max(rightsum)
		g remain=totalsum-rightsum+fweight
		drop rightsum totalsum
		
		gen quarter=.
			replace quarter=1 if month>=1&month<=3
			replace quarter=2 if month>=4&month<=6
			replace quarter=3 if month>=7&month<=9
			replace quarter=4 if month>=10&month<=12	
			
		*table month quarter
		label define QUARTER 1 "Jan.-Mar." 2 "Apr.-Jun." 3 "Jul.-Sep." 4 "Oct.-Dec."
		label value quarter QUARTER	
		
		
		// Original code which is too slow
		/*
		g remain2=0
		local i=1
		while `i'<=10 {
		egen rightsum=sum(fweight*(wagcat>=`i')),by(prov year quarter agegrp edgrp mincat mincatn mwchg)
		*gen hzd=fweight/rightsum
		replace remain2=rightsum if cut==`i'
		*replace h1=hzd if cut==`i'
		gen byte bin`i'=cut==`i'
		drop rightsum 
		local i=`i'+1
		}
		*/
		local i=1
		while `i'<=164 {
			gen byte bin`i'=cut==`i'
			local i=`i'+1
		}		

		compress

		save $resultdir\data_joiners_`sex'_`k'_`j'_v1,replace


		local i=1
		while `i'<=5 {
		*Variables used in regression
		gen byte p10_`i'=prov==10 & part0 ==`i' 
		gen byte p11_`i'=prov==11 & part0 ==`i' 
		gen byte p12_`i'=prov==12 & part0 ==`i' 
		gen byte p13_`i'=prov==13 & part0 ==`i' 
		gen byte p24_`i'=prov==24 & part0 ==`i' 
		gen byte p46_`i'=prov==46 & part0 ==`i' 
		gen byte p47_`i'=prov==47 & part0 ==`i' 
		gen byte p48_`i'=prov==48 & part0 ==`i' 
		gen byte p59_`i'=prov==59 & part0 ==`i' 

		gen byte a1_`i'=agegrp==1& part0==`i'
		gen byte a2_`i'=agegrp==2& part0==`i'
		gen byte a3_`i'=agegrp==3& part0==`i'
		gen byte a5_`i'=agegrp==5& part0==`i'

		gen byte e1_`i'=edgrp==1& part0==`i'
		gen byte e2_`i'=edgrp==2& part0==`i'
		gen byte e4_`i'=edgrp==4& part0==`i'

		gen byte q1_`i'=quarter==1& part0==`i'
		gen byte q2_`i'=quarter==2& part0==`i'
		gen byte q4_`i'=quarter==4& part0==`i'

		*Commented out 1996 and 2012 for public use files.
		*gen byte y96_`i'=year==1996 & part0 ==`i' 
		gen byte y97_`i'=year==1997 & part0 ==`i' 
		gen byte y98_`i'=year==1998 & part0 ==`i' 
		gen byte y00_`i'=year==2000 & part0 ==`i'
		gen byte y01_`i'=year==2001 & part0 ==`i' 
		gen byte y02_`i'=year==2002 & part0 ==`i' 
		gen byte y03_`i'=year==2003 & part0 ==`i' 
		gen byte y04_`i'=year==2004 & part0 ==`i'
		gen byte y05_`i'=year==2005 & part0 ==`i' 
		gen byte y06_`i'=year==2006 & part0 ==`i' 
		gen byte y07_`i'=year==2007 & part0 ==`i' 
		gen byte y08_`i'=year==2008 & part0 ==`i'
		gen byte y09_`i'=year==2009 & part0 ==`i' 
		gen byte y10_`i'=year==2010 & part0 ==`i' 
		gen byte y11_`i'=year==2011 & part0 ==`i' 
		gen byte y12_`i'=year==2012 & part0 ==`i'
		gen byte y13_`i'=year==2013 & part0 ==`i'
		gen byte y14_`i'=year==2014 & part0 ==`i'
		gen byte y15_`i'=year==2015 & part0 ==`i'
		gen byte y16_`i'=year==2016 & part0 ==`i'

		/*****TL: ADD PROVINCIAL TRENDS****/
		*gen byte ont_`i'=prov==35 & part0 ==`i' 
		gen tr10_`i'=(year-1999)*p10_`i' 
		gen tr11_`i'=(year-1999)*p11_`i' 
		gen tr12_`i'=(year-1999)*p12_`i' 
		gen tr13_`i'=(year-1999)*p13_`i' 
		gen tr24_`i'=(year-1999)*p24_`i' 
		*gen tr35_`i'=(year-1999)*ont_`i' 
		gen tr46_`i'=(year-1999)*p46_`i' 
		gen tr47_`i'=(year-1999)*p47_`i' 
		gen tr48_`i'=(year-1999)*p48_`i' 
		gen tr59_`i'=(year-1999)*p59_`i' 

		gen tq10_`i'=(year-1999)^2*p10_`i' 
		gen tq11_`i'=(year-1999)^2*p11_`i' 
		gen tq12_`i'=(year-1999)^2*p12_`i' 
		gen tq13_`i'=(year-1999)^2*p13_`i' 
		gen tq24_`i'=(year-1999)^2*p24_`i' 
		*gen tq35_`i'=(year-1999)^2*ont_`i' 
		gen tq46_`i'=(year-1999)^2*p46_`i' 
		gen tq47_`i'=(year-1999)^2*p47_`i' 
		gen tq48_`i'=(year-1999)^2*p48_`i' 
		gen tq59_`i'=(year-1999)^2*p59_`i' 

		/******ADD PROV*BIN AND YEAR*BIN CONTROLS*****/
		gen bp10_`i'=(wagcat-(14+25*`i'))*p10_`i' 
		gen bp11_`i'=(wagcat-(14+25*`i'))*p11_`i' 
		gen bp12_`i'=(wagcat-(14+25*`i'))*p12_`i' 
		gen bp13_`i'=(wagcat-(14+25*`i'))*p13_`i' 
		gen bp24_`i'=(wagcat-(14+25*`i'))*p24_`i' 
		gen bp46_`i'=(wagcat-(14+25*`i'))*p46_`i' 
		gen bp47_`i'=(wagcat-(14+25*`i'))*p47_`i' 
		gen bp48_`i'=(wagcat-(14+25*`i'))*p48_`i' 
		gen bp59_`i'=(wagcat-(14+25*`i'))*p59_`i' 

		*gen by96_`i'=(wagcat-(14+25*`i'))*y96_`i'
		gen by97_`i'=(wagcat-(14+25*`i'))*y97_`i' 
		gen by98_`i'=(wagcat-(14+25*`i'))*y98_`i' 
		gen by00_`i'=(wagcat-(14+25*`i'))*y00_`i' 
		gen by01_`i'=(wagcat-(14+25*`i'))*y01_`i' 
		gen by02_`i'=(wagcat-(14+25*`i'))*y02_`i' 
		gen by03_`i'=(wagcat-(14+25*`i'))*y03_`i' 
		gen by04_`i'=(wagcat-(14+25*`i'))*y04_`i' 
		gen by05_`i'=(wagcat-(14+25*`i'))*y05_`i' 
		gen by06_`i'=(wagcat-(14+25*`i'))*y06_`i' 
		gen by07_`i'=(wagcat-(14+25*`i'))*y07_`i' 
		gen by08_`i'=(wagcat-(14+25*`i'))*y08_`i' 
		gen by09_`i'=(wagcat-(14+25*`i'))*y09_`i' 
		gen by10_`i'=(wagcat-(14+25*`i'))*y10_`i' 
		gen by11_`i'=(wagcat-(14+25*`i'))*y11_`i' 
		gen by12_`i'=(wagcat-(14+25*`i'))*y12_`i' 
		gen by13_`i'=(wagcat-(14+25*`i'))*y13_`i' 
		gen by14_`i'=(wagcat-(14+25*`i'))*y14_`i' 
		gen by15_`i'=(wagcat-(14+25*`i'))*y15_`i' 
		gen by16_`i'=(wagcat-(14+25*`i'))*y16_`i' 


		local i=`i'+1
		}

		/*****TL: ADD A FULL SET OF prov*year DUMMIES (SEGMENT 1)***/
		gen provyear=100*prov+(year-1997) if part0==1
		replace provyear=9999 if part0>=2
		quietly tab provyear, gen(py)

		/*NOTE: ONTARIO IN 1999 IS py103*/
		/*Note: without 1996, py200 is the last valid dummy (py201 is for obs in the second and higher partition */

		gen cmin6b=mwchg*min6b1
		gen cmin35b=mwchg*min35b1
		gen cmin12b=mwchg*min12b1
		gen cmin=mwchg*min1
		gen cminold2new=mwchg*minold2new
		gen cminnew=mwchg*minnew
		gen cmin12a=mwchg*min12a2
		gen cmin35a=mwchg*min35a2
		gen cmin610a=mwchg*min610a2
		gen cmin1115a=mwchg*min1115a2
		gen cmin1620a=mwchg*min1620a2
		gen cmin2125a=mwchg*min2125a2
		gen cmin2630a=mwchg*min2630a2
		gen cmin3135a=mwchg*min3135a2
		gen cmin3640a=mwchg*min3640a2
		gen cmin4145a=mwchg*min4145a2
		gen cmin4650a=mwchg*min4650a2
		
		gen dolm=dollar*min1

		

egen est_nobs=sum(one)
sum est_nobs


		glm fweight min1 min6b1 min35b1 min12b1 min12a1 min35a1 min610a1 min1115a1 min1620a1 min2125a1 min2630a1   ///
		min3135a1 min3640a1 min4145a1 min4650a1 ///
		cmin cminold2new cminnew cmin6b cmin35b cmin12b cmin12a cmin35a ///
		cmin610a cmin1115a cmin1620a cmin2125a cmin2630a ///
		cmin3135a cmin3640a cmin4145a cmin4650a dollar ///
		 a*_1 e*_1 py1-py102 py104-py200 q*_1 bp*_1 by*_1 ///
		 a*_2 e*_2 p*_2 y*_2 q*_2 tr*_2 tq*_2 bp*_2 by*_2 ///
		 a*_3 e*_3 p*_3 y*_3 q*_3 tr*_3 tq*_3 bp*_3 by*_3 ///
		 a*_4 e*_4 p*_4 y*_4 q*_4 tr*_4 tq*_4 bp*_4 by*_4 ///
		 a*_5 e*_5 p*_5 y*_5 q*_5 tr*_5 tq*_5 bp*_5 by*_5 ///
		 bin* if remain>0 &wagcat~=164 , link(cloglog) family(binomial remain) vce(cluster prov)

		est save $resultdir\joiners_`sex'_`k'_`j'_v1_5,replace
		
		*Min only up to $4.50
		test min4650a1 cmin4650a
		*Min only up to $4.00
		test min4145a1 min4650a1 cmin4145a cmin4650a
		*Min only up to $3.50
		test min3640a1 min4145a1 min4650a1 cmin3640a cmin4145a cmin4650a
		*Min only up to $3.00
		test min3135a1 min3640a1 min4145a1 min4650a1 cmin3135a cmin3640a cmin4145a cmin4650a
		*Min only up to $2.50
		test min2630a1 min3135a1 min3640a1 min4145a1 min4650a1 cmin2630a cmin3135a cmin3640a cmin4145a cmin4650a
		*Min only up to $2.00.
		test min2125a1 min2630a1 min3135a1 min3640a1 min4145a1 min4650a1 cmin2125a cmin2630a cmin3135a cmin3640a cmin4145a cmin4650a

		
erase $resultdir\data_joiners_`sex'_`k'_`j'_v1.dta

} /*End j-loop  */
	 

	*local k=`k'+1
	*}

	local sex=`sex'+1
}


log close




